package com.example.viewpager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;
    LinearLayout linearLayout;
    List<ImageView> imageViewList=new ArrayList<ImageView>();
    int currentItem=300;//当前item位置
    Timer mTimer;
    int[] resId={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.b};
    Handler myHandler=new Handler();

    TimerTask timerTask=new TimerTask() {
        @Override
        public void run() {
            myHandler.post(runnable);
        }
    };

    private void start() {
        if (mTimer==null){
            mTimer=new Timer();
        }
        mTimer.schedule(timerTask,3000,3000);
    }

    /**
     * 更新UI操作
     */
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            currentItem++;
            int index=currentItem%4;
            updatePoint(index);//初始化小圆点
            viewPager.setCurrentItem(currentItem);
        }
    };
    /**
     * 初始化小圆点（如果不需要小圆点则不加）
     */
    private void initPoint(int i) {
        ImageView point=new ImageView(this);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20,20);
        point.setLayoutParams(params);
        if (i==0){
            point.setImageResource(R.drawable.point_checked);
        }else {
            point.setImageResource(R.drawable.point_normal);
        }
        linearLayout.addView(point);
    }

    /**
     * 更新小圆点（如果不需要小圆点则不加）
     * @param index
     */
    private void updatePoint(int index) {
        for (int i=0;i<imageViewList.size();i++){
            ImageView image= (ImageView) linearLayout.getChildAt(i);
            if (i==index){
                image.setImageResource(R.drawable.point_checked);
            }else {
                image.setImageResource(R.drawable.point_normal);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout=(LinearLayout)findViewById(R.id.point);
        viewPager=(ViewPager)findViewById(R.id.viewpager);
        for (int i=0;i<resId.length;i++){
            ImageView imageView=new ImageView(this);
            imageView.setImageResource(resId[i]);
            imageViewList.add(imageView);
            initPoint(i); //圆点
        }
        MyAdapter adapter=new MyAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(currentItem); //一定要在setAdapter后面
        start();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            /**
             * 更新当前item数目（currentPosition）转换item时调用此方法
             * @param position
             */
            @Override
            public void onPageSelected(int position) {
                currentItem=position;
                int index=position%imageViewList.size();
                updatePoint(index);//更新小圆点
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mTimer!=null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
    public class MyAdapter extends PagerAdapter {
        public MyAdapter() {
            super();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            int index = position % imageViewList.size();    //循环imageViewList里面的内容
            ((ViewPager) container).addView(imageViewList.get(index));
            return imageViewList.get(index);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView(imageViewList.get(position % imageViewList.size()));
        }

        /**
         * 设定tab数目为Integer.MAX_VALUE
         *
         * @return
         */
        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        myHandler.removeCallbacks(runnable);
    }
}